package countDownLanuch;

import java.util.concurrent.CountDownLatch;


/**
 * 
 * 
 * CountDownLatch 这是一个计数器的通知 ，刚开始设置一个变量，在一个线程中执行等待
 * 当别的线程都执行完毕 ，都会计数器减1，并且通知一下等待的线程 ，判断是否计数器是0，如果是0那么等待线程唤醒，开始执行
 * 
 * 
 * @author Administrator
 *
 */
public class CountDownLanuchTest {
	
	public static void main(String[] args) {
		
		
		final CountDownLatch countDown = new CountDownLatch(2);
		
		
		Thread t1 =  new Thread(new Runnable() {
			public void run() {
				try {
					System.out.println("进入线程T1....等待其他线程完成!");
					countDown.await();
					System.out.println("收到通知，计数器是0，T1继续执行..");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"t1");
		
		
		Thread t2 =  new Thread(new Runnable() {
			public void run() {
				try {
					System.out.println("进入线程T2...");
					Thread.sleep(3000);//睡眠3s
					System.out.println("T2执行完成，计数器减1..通知T1线程");
					
					countDown.countDown();//执行计数器减1
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"t2");
		
		
		
		Thread t3 =  new Thread(new Runnable() {
			public void run() {
				try {
					System.out.println("进入线程T3...");
					Thread.sleep(3000);//睡眠3s
					System.out.println("T3执行完成，计数器减1..通知T1线程");
					
					countDown.countDown();//执行计数器减1
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"t3");
		
		
		t1.start();
		t2.start();
		t3.start();
	}
	
	

}
